<template>
  <el-container class="home_container">
    <el-header>
      <div>
          <img src="../assets/user.png">
          <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">logout</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#409eff" unique-opened :collapse="isCollapse" :collapse-transition="false"
        :router="true" :default-active="this.$route.path">
          <!--            一级菜单-->
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
<!--            一级菜单的模版区域-->
            <template slot="title">
<!--              图标-->
              <i :class="item.icon"></i>
<!--              文本-->
              <span>{{item.menuName}}</span>
            </template>
              <el-menu-item :index="subItem.path" v-for="subItem in item.children" :key="subItem.id">
                <template slot="title">
                  <!--              图标-->
                  <i class="el-icon-menu"></i>
                  <!--              文本-->
                  <span>{{subItem.menuName}}</span>
                </template>
              </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      menuList: [],
      isCollapse: false
    }
  },
  created () {
    this.getMenu()
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenu () {
      const { data: res } = await this.$http({
        url: '/menus',
        method: 'get'
      })
      if (res.status !== 200) return this.$message.error('获取菜单信息失败')
      this.menuList = res.data
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    }
    // 保存链接的激活状态，以便在刷新的时候加载
    // saveNavState (activePath) {
    //   window.sessionStorage.setItem('activePath', activePath)
    // },
    // getActivePath () {
    //   return window.sessionStorage.getItem('activePath')
    // }
  }
}
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;
}
.el-header {
  background-color: rgb(255, 255, 255);
  padding-left: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #323131;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    > span {
      margin-left: 5px;
    }
  }
}
.el-aside {
  background-color: rgb(64, 64, 64);
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #f8f7f7;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  height: 24px;
  line-height: 24px; //文本在行高中来居中
  color: #446ab6;
  text-align: center;
  letter-spacing: .5em;
  cursor: pointer;
}
</style>
